/*Copyright (c) 2011 Guillermo Quiros

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the"Software"), 
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ogelframework.helpers
{
	import org.ogelframework.core.IDispatcher;
	import org.ogelframework.core.IInjector;
	import org.ogelframework.core.Component;
	import org.ogelframework.core.Const;
	import org.ogelframework.registry.Entity;
	import org.ogelframework.registry.Registry;

	public class Binder
	{
		private var _registry:Registry;
		private var _watcher:Watcher;
		
		public function Binder(registry:Registry,watcher:Watcher)
		{
			super();
			_registry=registry;
			_watcher=watcher;
		}

		
		public function bind(entity:Entity):void
		{			
			bindDependencies(entity);
			bindDependents(entity);
		}
		
		private function bindDependencies(entity:Entity):void
		{
			var dependency:Array=_registry.dependencies.getDependencies(entity.id);
			for (var id:String in dependency)
				doBind(dependency[id],true);
		}
		
		private function bindDependents(entity:Entity):void
		{
			var dependency:Array=_registry.dependencies.getDependens(entity.id);
			for (var id:String in dependency)
				doBind(dependency[id],true);
		}
		
		private function doBind(value:String,forceInjection:Boolean=false):void
		{
			var arg:Array=value.split('#');
			var type:String=arg[0];
			var sourceKey:String=arg[1];
			var destinationKey:String=arg[2];
			var source:Entity=_registry.getEntity(sourceKey);
			var destination:Entity=_registry.getEntity(destinationKey);
			trace("Dobind"+value)
			
			if (!source)
			{
				trace('no entity source for '+sourceKey);
				return;
			}
			if ((source.instance==null)&&(type==Const.INJECTION)&&(forceInjection))
			{
				trace('CREATION OF instance '+source.id);
				source.instance=new source.clazz();
				bind(source);
				return;
			}
			
			if (!destination)
			{
				trace('no entity destination '+destinationKey);
				return;
			}

			switch (type)
			{
				case Const.INJECTION:
					doInjection(sourceKey,destinationKey)
					break;
				case Const.HANDLER:
					doHandler(sourceKey,destinationKey,arg[3])
					break;
				case Const.LINK:
					doLink(sourceKey,destinationKey,false)
					break;
				case Const.DISPATCHER:
					doDispatcher(sourceKey,destinationKey)
					break;
				case Const.INJECTOR:
					doInjector(sourceKey,destinationKey);
					break;
				default:
					 trace("not handlel"+ type)
					break
			}
			
		}
		
		private function doInjection(source:String,destination:String):void
		{
			var objSource:Object=getObject(source);
			assignObj(destination,objSource)	
			trace('doInjection');
		}
		
		private function doHandler(source:String,destination:String,event:String):void
		{
			trace('doHandler');
			var objSource:Object=getObject(source);
			var listener:Object=getObject(destination);
			if (!listener is Function)
				trace('Observer is not a function ');
			if (!objSource)
			{
				trace('No Source ');
				return;
			}
			if(objSource.hasOwnProperty('addEventListener'))
				objSource.addEventListener(event,listener);
			
		}
		
		private function doLink(source:String,destination:String,bidirectional:Boolean):void
		{
			_watcher.add(source,destination);
		}
		
		private function doDispatcher(source:String,destination:String):void
		{
			trace('doDispatcher');
			var objSource:Object=getObject(source);
			if (objSource is Component)
			assignObj(destination,Component(objSource).dispatcher)	
		}
		
		private function doInjector(source:String,destination:String):void
		{
			trace('doInjector');
			var objSource:Object=getObject(source);
			if (objSource is IInjector)
				assignObj(destination,IInjector(objSource))	
		}
		
		
		private function getObject(key:String):Object
		{
			var source:Entity=_registry.getEntity(key);
			var arg:Array=key.split('.');
			var result:Object=source.instance;
			arg=arg.slice(1);
			for each(var prop:String in arg)
			{
				if (result==null)
					break;
				result=result[prop];
			}
			return result;			
		}
		
		private function assignObj(key:String,obj:Object):void
		{
			var entity:Entity=_registry.getEntity(key);
			var result:Object=entity.instance;
			var arg:Array=key.split('.');
			var prop:String=arg[arg.length-1];
			arg.pop();
			for (var i:int;i<arg.length-1;i++)
				result=result[arg];
			if (result==null) return;
			result[prop]=obj;
		}
	}
}
